package com.scau.action.business;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;
import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.BomMaterial;
import com.scau.model.business.ModelRule;
import com.scau.model.business.Shoe;
import com.scau.service.impl.business.BomMaterialService;
import com.scau.service.impl.business.ModelRuleService;
import com.scau.service.impl.business.ShoeService;
import com.scau.util.PageController;

@Component
@Scope("prototype")
public class BomAction extends BaseAction implements Serializable ,ModelDriven<Shoe> {
	private final static Log logger = LogFactory.getLog(BomAction.class);
	private PageController pager;
	private ShoeService shoeService;
	private Shoe shoe;
	private List<Shoe> shoeList = null;
	private ModelRuleService modelRuleService;
	private BomMaterialService bomMaterialService;
	
	public String list() {
		// 显示列表
		int totalRows = shoeService.getRecordCount(new Shoe());
		String URL = getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);
		shoeList = shoeService.list(new Shoe(), this.pager
				.getPageStartRow(), pager.getPageSize(),
				null,null);
		List<ModelRule> modelList = modelRuleService
				.listAll(new ModelRule());
		request.setAttribute("modelList", modelList);
		pager.setData(shoeList);
		return "list";
	}
	
	public String get() {
		shoe = shoeService.get(shoe);
		List<ModelRule> modelList = (new ModelRuleService())
				.listAll(new ModelRule());
		request.setAttribute("modelList", modelList);
		return "edit";
	}

	public String save() {
		try {
			shoeService.save(shoe);
			return "list";
		} catch (BusinessException e) {
			// 保存原来表单已输入的内容
			request.setAttribute("shoe", shoe);
			request.setAttribute("message", e.getMessage());
			return "edit";
		}
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String del() {
		String[] ids = request.getParameterValues("id");
		for (String id : ids) {
			Shoe s = new Shoe();
			BomMaterial bm = new BomMaterial();
			if (null != id && !("".equals(id))) {
				bm.setShoe_id(Long.valueOf(id));
				bomMaterialService.delete(bm);
				s.setId(Long.parseLong(id));
				shoeService.delete(s);
			}
		}
		return "list";
	}
	

	public PageController getPager() {
		return pager;
	}

	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}


	public Shoe getShoe() {
		return shoe;
	}

	public void setShoe(Shoe shoe) {
		this.shoe = shoe;
	}

	public List<Shoe> getShoeList() {
		return shoeList;
	}

	public void setShoeList(List<Shoe> shoeList) {
		this.shoeList = shoeList;
	}

	public ShoeService getShoeService() {
		return shoeService;
	}

	public ModelRuleService getModelRuleService() {
		return modelRuleService;
	}

	public BomMaterialService getBomMaterialService() {
		return bomMaterialService;
	}

	@Resource
	public void setShoeService(ShoeService shoeService) {
		this.shoeService = shoeService;
	}

	@Resource
	public void setModelRuleService(ModelRuleService modelRuleService) {
		this.modelRuleService = modelRuleService;
	}

	@Resource
	public void setBomMaterialService(BomMaterialService bomMaterialService) {
		this.bomMaterialService = bomMaterialService;
	}

	@Override
	public Shoe getModel() {
		// TODO Auto-generated method stub
		return shoe;
	}

}
